package com.aloda.service.impl;

import com.aloda.constant.UserConstant;
import com.aloda.mapper.RecordMapper;
import com.aloda.pojo.dto.RecordPageDTO;
import com.aloda.pojo.dto.UserSafetyDTO;
import com.aloda.pojo.entity.Record;
import com.aloda.result.PageResult;
import com.aloda.service.RecordService;
import com.aloda.utils.UserHolder;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    /**
     * 分页查询借阅记录
     *
     * @param recordPageDTO
     * @return
     */
    @Override
    public PageResult<Record> getRecordPage(RecordPageDTO recordPageDTO) {
        PageHelper.startPage(recordPageDTO.getCurrentPage(),recordPageDTO.getPageSize());
        // 如果是普通用户则只能查询自己的图书借阅记录
        UserSafetyDTO user = UserHolder.getUser();
        if (UserConstant.USER_USER.equals(user.getRole())) {
            recordPageDTO.setBorrower(user.getName());
        }
        Page page = recordMapper.selectPage(recordPageDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
